/*     */ package org.gd.hellocrypt;
/*     */
/*     */ public class Base32
/*     */ {
/*     */   private static final String base32Chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ234567";
/*  18 */   private static final int[] base32Lookup = { 255, 255, 26, 27, 28, 29, 30, 31, 255, 255, 255, 255, 255, 255, 255, 255, 255, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 255, 255, 255, 255, 255, 255, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 255, 255, 255, 255, 255 };
/*     */
/*     */   public static String encode(byte[] bytes)
/*     */   {
/*  33 */     int i = 0; int index = 0; int digit = 0;
/*     */
/*  35 */     StringBuffer base32 = new StringBuffer((bytes.length + 7) * 8 / 5);
/*     */
/*  37 */     while (i < bytes.length)
/*     */     {
/*  39 */       int currByte = (bytes[i] >= 0) ? bytes[i] : bytes[i] + 256;
/*     */
/*  42 */       if (index > 3)
/*     */       {
/*     */         int nextByte;
/*  44 */         if (i + 1 < bytes.length)
/*  45 */           nextByte = (bytes[(i + 1)] >= 0) ? bytes[(i + 1)] : bytes[(i + 1)] + 256;
/*     */         else {
/*  47 */           nextByte = 0;
/*     */         }
/*  49 */         digit = currByte & 255 >> index;
/*  50 */         index = (index + 5) % 8;
/*  51 */         digit <<= index;
/*  52 */         digit |= nextByte >> 8 - index;
/*  53 */         ++i;
/*     */       }
/*     */       else
/*     */       {
/*  57 */         digit = currByte >> 8 - (index + 5) & 0x1F;
/*  58 */         index = (index + 5) % 8;
/*  59 */         if (index == 0)
/*  60 */           ++i;
/*     */       }
/*  62 */       base32.append("ABCDEFGHIJKLMNOPQRSTUVWXYZ234567".charAt(digit));
/*     */     }
/*     */
/*  65 */     return base32.toString();
/*     */   }
    /*     */
/*     */   public static byte[] decode(String base32)
/*     */   {
/*  71 */     byte[] bytes = new byte[base32.length() * 5 / 8];
/*     */
/*  73 */     int i = 0; int index = 0; for (int offset = 0; i < base32.length(); ++i)
/*     */     {
/*  75 */       int lookup = base32.charAt(i) - '0';
/*     */
/*  78 */       if (lookup < 0) continue; if (lookup >= base32Lookup.length) {
/*     */         continue;
/*     */       }
/*  81 */       int digit = base32Lookup[lookup];
/*     */
/*  84 */       if (digit == 255) {
/*     */         continue;
/*     */       }
/*  87 */       if (index <= 3)
/*     */       {
/*  89 */         index = (index + 5) % 8;
/*  90 */         if (index == 0)
/*     */         {
/*     */           int tmp90_88 = offset;
/*     */           byte[] tmp90_86 = bytes; tmp90_86[tmp90_88] = (byte)(tmp90_86[tmp90_88] | digit);
/*  93 */           ++offset;
/*  94 */           if (offset < bytes.length) continue; break;
/*     */         }
/*     */         int tmp115_113 = offset;
/*     */         byte[] tmp115_111 = bytes; tmp115_111[tmp115_113] = (byte)(tmp115_111[tmp115_113] | digit << 8 - index);
/*     */       }
/*     */       else
/*     */       {
/* 101 */         index = (index + 5) % 8;
/*     */         int tmp141_139 = offset;
/*     */         byte[] tmp141_137 = bytes; tmp141_137[tmp141_139] = (byte)(tmp141_137[tmp141_139] | digit >>> index);
/* 103 */         ++offset;
/*     */
/* 105 */         if (offset >= bytes.length)
/*     */           break;
/*     */         int tmp168_166 = offset;
/*     */         byte[] tmp168_164 = bytes; tmp168_164[tmp168_166] = (byte)(tmp168_164[tmp168_166] | digit << 8 - index);
/*     */       }
/*     */     }
/* 109 */     return bytes;
/*     */   }
/*     */ }